<?php

namespace addons\TinyShare\services;

use common\components\Service;
use common\enums\StatusEnum;
use addons\TinyShare\common\models\ShareAdv;

/**
 * Class AdvService
 * @package addons\TinyShare\services
 * @author jackey <270499458@qq.com>
 */
class AdvService extends Service
{
    /**
     * 获取广告列表
     *
     * @param array $locals
     * @return array
     */
    public function getListByLocal($local)
    {
        if (empty($local)) {
            return $local;
        }

        return ShareAdv::find()
            ->where(['status' => StatusEnum::ENABLED])
            ->andWhere(['location_id' => $local])
            ->andWhere(['<', 'start_time', time()])
            ->andWhere(['>', 'end_time', time()])
            ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
            ->orderBy('sort asc, id desc')
            ->cache(60)
            ->asArray()
            ->all();
    }
}